var section = {
	init : function( ){
		$.getScriptSafe('/static/settings.js', section.loadMlPack);
	},
	
	loadMlPack : function(){
		$.getScriptSafe('/static/js/lib/ml-utils.js', section.allLoadsComplete);
	},
	
	allLoadsComplete: function () {
		$('.nav-list-banks').bind('click', function (e) {
			e.preventDefault();
			
			mlModal.open({
				width: 980,
				height: 380,
				title: 'Procurar número do banco',
				urlContent: '/static/templates/modal-cadastro-bancos.html',
				loadComplete: section.mount
			});
		});
		
		$('.pagination a.fR').bind('click', section.postForm);
	},
	
	postForm: function(e){
		e.preventDefault();

		var isValid = true;
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input#id_bank_code').val() == ''){
			$('input#id_bank_code').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input#id_bank_code').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input#id_bank_agency').val() == ''){
			$('input#id_bank_agency').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input#id_bank_agency').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input#id_bank_account').val() == ''){
			$('input#id_bank_account').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input#id_bank_account').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input#id_pis').val() == ''){
			$('input#id_pis').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input#id_pis').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input#id_approval:checked').length == 0){
			$('input#id_approval').parent().addClass('error');
			isValid = false;
		}else{
			$('input#id_approval').parent().removeClass('error');
		}
		
		
		

		if(isValid){
			trace('Ok')
			$('form').submit();
		}else{
			if($('.input.error').length == 0) return;	
			
			$('html, body').animate({
				scrollTop: $('.input.error:eq(0)').offset().top - 50
			}, {
            duration: 800,
            queue: false
        });
		  return; 
		}
	},
	
	mount: function() {
		section.listBanks();
		
		$('.modal-banks .search-banks .nav-search-ok').bind('click', function (e) {
			e.preventDefault();
			var param = $('.modal-banks .search-banks .form input').val();
			if(param.length == 0) return false;
			section.searchBanks(param);
		})
		
		$('.modal-banks input.text').wrap($('<div/>').addClass('bx-input'));
        $('.bx-input').append($('<span/>').addClass('input'));
        $('.input')
            .append($('<span/>').addClass('fL'))
            .append($('<span/>').addClass('fR'));
	},
	
	listBanks : function(){
		$.ajax({
			type: 'post',
			dataType: 'json',
			contentType: 'application/json',
			url: settings.bank.fetch,
			success: section.listBanksComplete
		});
	},
	
	listBanksComplete : function(data, xhr, stt){
		var list = data;
		$.each( list.bank, function (i, bank) {
			section.getTrItem(bank, i).appendTo($('.modal-banks .list-banks .tb-body'));
		});
	},
	
	searchBanks : function(data){
		$.ajax({
			type: 'post',
			dataType: 'json',
			contentType: 'application/json',
			processData: false,
			data: new mlSerializer().add('param', data).render(),
			url: settings.bank.searchFetch,
			success: section.searchBanksComplete
		});
	},
	
	searchBanksComplete : function(data, xhr, stt){
		$('.modal-banks .search-banks table').empty();
		
		var list = data;
		$.each( list.bank, function (i, bank) {
			section.getTrItem(bank, i).appendTo($('.modal-banks .search-banks table'));
		});
	},
	
	getTrItem: function (bank, i) {
		var tr = $('<tr/>');
		
		var td1 = $('<td/>')
			.addClass('col-1')
			.text(bank.number)
			.appendTo(tr);
		
		var td2 = $('<td/>')
			.addClass('col-2')
			.text(bank.name)
			.appendTo(tr);
		
		var td3 = $('<td/>')
			.addClass('col-3')
			.appendTo(tr);
		
		var navSel = $('<a/>')
			.attr({
				title: 'Selecionar',
				href: '#'
			})
			.text('Selecionar')
			.bind('click', function (e) {
				e.preventDefault();
				$('.number-bank input').val(bank.number);
				mlModal.close();
			})
			.appendTo(td3);	

		if(i % 2) tr.addClass('alter');
		
		return tr;
	}
};

$(document).ready( section.init );